import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import dayjs from "dayjs";

/**
 * 加载分页数据Hook
 *
 * @param {Function} onAsyncFunc 异步请求方法
 * @param {Object} values 初始化参数
 * @returns {Object} dataSource 数据源
 */
const useLoadPageData = (onAsyncFunc, values) => {
  const valueRef = useRef(values).current;
  const initValues = useMemo(() => ({ ...valueRef, params: { pageNum: 1, pageSize: 20 } }), [valueRef]);
  const [loading, setLoading] = useState(false);
  const [dataSource, setDataSource] = useState([]);
  const [pagination, setPagination] = useState(initValues.params);
  const [search, setSearch] = useState({});

  // 获取数据
  const onGetData = useCallback(
    async (props = {}) => {
      if (onAsyncFunc) {
        setLoading(true);
        // const { params, ...resProps } = props || {};
        // const { params: initParams, ...resInitValues } = initValues;
        // search.params = { ...initParams, ...search.params, ...params };
        // const options = { ...resInitValues, ...search, ...resProps };
        const values = { ...initValues, ...search, ...props };
        const params = { ...initValues.params, ...search.params, ...props.params };
        const options = { ...values, params };
        const { content, ...page } = await onAsyncFunc(options).finally(() => setLoading(false));
        setPagination(page);
        setDataSource(content);
      }
    },
    [initValues, search, onAsyncFunc]
  );

  useEffect(() => {
    onGetData();
  }, [onGetData]);

  // 搜索数据
  const onSearchData = useCallback(
    (values) => {
      if (values?.createTimes) {
        let [startTime, endTime] = values.createTimes;
        startTime = dayjs(startTime).format("YYYY-MM-DD");
        endTime = dayjs(endTime).format("YYYY-MM-DD");
        values.params = { startTime, endTime };
        Reflect.deleteProperty(values, "createTimes");
      }
      if (values?.updateTimes) {
        let [startUpdateTime, endUpdateTime] = values.createTimes;
        startUpdateTime = dayjs(startUpdateTime).format("YYYY-MM-DD");
        endUpdateTime = dayjs(endUpdateTime).format("YYYY-MM-DD");
        values.params = { startUpdateTime, endUpdateTime };
        Reflect.deleteProperty(values, "updateTimes");
      }
      values.params = { ...values.params, pageNum: 1, pageSize: pagination.pageSize };
      setPagination({ ...pagination, pageNum: 1 });
      setSearch(values);
    },
    [pagination]
  );

  // 重置搜索数据
  const onResetData = useCallback(() => {
    setPagination({ ...pagination, pageNum: 1 });
    setSearch({ params: { pageNum: 1, pageSize: pagination.pageSize } });
  }, [pagination]);

  // 刷新数据
  const onRefreshData = useCallback(() => {
    const { pageNum, pageSize, totalPages } = pagination;
    if (pageNum > 2 && totalPages % pageSize === 0) {
      return setPagination({ ...pagination, pageNum: pageNum - 1 });
    }
    onGetData({ params: { pageNum, pageSize } });
  }, [pagination, onGetData]);

  // 分页切换
  const onPageChange = useCallback(
    (pageNum, pageSize) => {
      setPagination({ ...pagination, pageNum, pageSize });
      onGetData({ params: { pageNum, pageSize } });
    },
    [pagination, onGetData]
  );

  return {
    loading,
    dataSource,
    pagination,
    onGetData,
    onSearchData,
    onResetData,
    onRefreshData,
    onPageChange,
  };
};

export default useLoadPageData;
